package com.dib.neo.bkfs.fs;

import java.nio.ByteBuffer;

public class BlockBuffer {
  private volatile long start;
  private final ByteBuffer buffer;

  public BlockBuffer(int blockSize) {
    this.buffer = ByteBuffer.allocate(blockSize);
  }

  public long getStart() {
    return start;
  }

  public void update(long start,ByteBuffer dst) {
    this.start = start;
    buffer.clear();
    buffer.put(dst);
  }



  public long getEnd(){
    return start+buffer.position()-1;
  }

  public int read(long posistion){
    if(canRead(posistion)){
      int index = (int)(posistion - start);
      return buffer.get(index);
    }else{
      return -1;
    }
  }

  public boolean canRead(long posistion) {
    return posistion>=start&&posistion<=this.getEnd();
  }

  public static void main(String[] args) {
    BlockBuffer b = new BlockBuffer(1024);
    System.out.println("b.start = " + b.start);
    System.out.println("b.getEnd() = " + b.getEnd());
    System.out.println("b.canRead(0) = " + b.canRead(0));

  }

}
